package org.example.algorithm.linkedlist;

public class RemoveNthFromEndSolution {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode front = head;
        for (int i=0;i<n;i++) {
            front = front.next;
        }
        ListNode dummy = new ListNode(0, head);
        ListNode cur = dummy;
        while (front != null) {
            front = front.next;
            cur = cur.next;
        }
        cur.next = cur.next.next;
        return dummy.next;
    }
}
